<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:15%;height: 70%;overflow-y: hidden"
  >
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <div class="table-operator" style="margin-top: 5px">
        <a-button @click="AddContacts" type="primary" icon="plus">新增</a-button>
      </div>
      <div>
        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :scroll="{ x: 1000, y: 800 }"
          :loading="loading"
          @change="handleTableChange"
        >
          <span slot="action" slot-scope="text, record">
            <a @click="myhandkeEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a-popconfirm title="确定删除吗?" @confirm="() => myhandleDelete(record.id)">
              <a>删除</a>
            </a-popconfirm>
          </span>
          <!-- 状态渲染模板 -->
          <template slot="customRenderFlag" slot-scope="enabled">
            <a-tag v-if="enabled" color="green">启用</a-tag>
            <a-tag v-if="!enabled" color="orange">禁用</a-tag>
          </template>
        </a-table>
      </div>
    </a-spin>
    <addStamp ref="modalForm" @ok="reModalFormOk"></addStamp>
  </a-modal>
</template>
<script>
import pick from 'lodash.pick'
import { addCustomer, checkSupplier } from '@/api/api'
import { getAction, deleteAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import addStamp from './addStamp'
import { autoJumpNextInput } from '@/utils/util'
export default {
  name: 'StampModel',
  mixins: [JeecgListMixin],
  components: {
    addStamp
  },
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      isReadOnly: true,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      // 表头
      columns: [
        { title: '生产单号', dataIndex: 'productionNum', width: 100, align: 'center' },

        { title: '钢印号', dataIndex: 'sealNum', width: 120, align: 'center' },

        {
          title: '操作',
          dataIndex: 'action',
          width: 100,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        lists: '/productionPlan/getSealByProductionNum',
        delete: '/productionPlan/removeSeal'
      }
    }
  },
  created() {},
  methods: {
    myhandleDelete: function(id) {
      if (!this.url.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      console.log(id)
      var that = this
      deleteAction(that.url.delete, { id: id }).then(res => {
        if (res.code === 200 || res.code === 10000) {
          that.reModalFormOk()
        } else {
          that.$message.warning(res.data.message)
        }
      })
    },
    //保存成功后刷新列表
    reModalFormOk() {
      this.getActionList(this.model)
    },
    //打开弹层
    AddContacts() {
      this.$refs.modalForm.add(this.model)
      this.$refs.modalForm.title = '新增'
      this.$refs.modalForm.disableSubmit = false
    },
    myhandkeEdit(record) {
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '编辑'
      this.$refs.modalForm.disableSubmit = false
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.getActionList(record)
    },
    //请求列表数据
    getActionList(record) {
      this.loading = true
      let params = {}
      //规格属性管理查询条件
      // params.contractId = record.id
      //页码字段未统一
      params.productionNum=record.productionNum
      console.log(record);
      params.current = this.ipagination.current
      params.currentPage = this.ipagination.current
      params.size = this.ipagination.pageSize
      params.pageSize = this.ipagination.pageSize
      getAction(this.url.lists, params).then(res => {
        if (res.code === 200 || res.code === 10000) {
          this.dataSource = res.data
          //   this.ipagination.total = res.data.total
          //   this.tableAddTotalRow(this.columns, this.dataSource)
        }
      })
      this.loading = false
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(this.model, values)
          if (this.model.beginNeedGet && this.model.beginNeedPay) {
            that.$message.warn('期初应收和期初应付不能同时输入')
            that.confirmLoading = false
            return
          }
          let obj
          if (!this.model.id) {
            obj = addCustomer(formData)
          } else {
            obj = addCustomer(formData)
          }
          obj
            .then(res => {
              if (res.code === 200 || res.code === 10000) {
                this.$message.success('保存成功')
                that.$emit('ok')
              } else {
                that.$message.warning(res.data.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateSupplierName(rule, value, callback) {
      let params = {
        name: value,
        type: '客户',
        id: this.model.id ? this.model.id : 0
      }
      checkSupplier(params).then(res => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback()
          } else {
            callback('名称已经存在')
          }
        } else {
          callback(res.data)
        }
      })
    }
  }
}
</script>
<style scoped></style>
